package com.njit;

/**
 * Created by ospip on 2017/11/29.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import db.db;
import entity.PersonEntity;
import java.util.*;

public class personselect extends JFrame {
    private JTable table;
    private MyTableModel tablemodel;
    private JButton b1, b2, b3, b4;
    private JToolBar tool;

    public personselect() {
        this.setSize(600, 300);
        this.setLayout(new BorderLayout());
        this.setTitle("员工信息");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(getOwner());
        tablemodel = getModel();
        table = new JTable(tablemodel);
        table.setPreferredScrollableViewportSize(new Dimension(500, 250));
        JScrollPane scroll = new JScrollPane(table);
        this.add(scroll, BorderLayout.CENTER);
        // Begin
        b1 = new JButton("Add");
        b1.setFocusable(false);
        b1.setHorizontalTextPosition(SwingConstants.CENTER);
        b1.setVerticalTextPosition(SwingConstants.CENTER);
        b2 = new JButton("Edit");
        b2.setFocusable(false);
        b2.setHorizontalTextPosition(SwingConstants.CENTER);
        b2.setVerticalTextPosition(SwingConstants.CENTER);
        b3 = new JButton("Delete");
        b3.setFocusable(false);
        b3.setHorizontalTextPosition(SwingConstants.CENTER);
        b3.setVerticalTextPosition(SwingConstants.CENTER);
        b4 = new JButton("Refresh");
        b4.setFocusable(false);
        b4.setHorizontalTextPosition(SwingConstants.CENTER);
        b4.setVerticalTextPosition(SwingConstants.CENTER);
        tool = new JToolBar();
        tool.add(b1); tool.add(b2); tool.add(b3); tool.add(b4);
        tool.setRollover(true);
        this.add(tool, BorderLayout.NORTH);
        b1.addActionListener((e) -> {
            new personadd().setVisible(true);
            dispose();
        });
        b2.addActionListener((e) -> {
            int index = 0, count;
            db dbcon = new db();
            if (table.getCellEditor() != null) {
                table.getCellEditor().stopCellEditing();
            }
            try {
                String sql = "update person set name=?, sex=?, birthday=?, professor=?, deptno=? where no=?;";
                PreparedStatement presta = dbcon.PreparedStatement(sql);
                count = tablemodel.getEditedIndex().size();
                if (count > 0) {
                    for (int i = 0; i < count; i++) {
                        index = tablemodel.getEditedIndex().get(i);
                        for (int j = 1; j <= 5; j++) {
                            presta.setString(j, table.getValueAt(index, j).toString());
                        }
                        presta.setString(6, table.getValueAt(index, 0).toString());
                        presta.addBatch();
                    }
                }
                presta.executeBatch();
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        b3.addActionListener((e) -> {
            db dbcon = new db();
            try {
                if (table.getSelectedRows().length > 0) {
                    int[] selRowIndexs = table.getSelectedRows();
                    PreparedStatement presta = dbcon.PreparedStatement("delete from person where no=?");
                    for (int i = 0; i < selRowIndexs.length; i++) {
//                        selRowIndexs[i] = selRowIndexs[i] - i;
                        presta.setString(1, table.getValueAt(selRowIndexs[i], 0).toString());
                        presta.addBatch();
                    }
                    presta.executeBatch();
                    tablemodel = getModel();
                    table.setModel(tablemodel);
                }
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        b4.addActionListener((e) -> {
            dispose();
            new personselect().setVisible(true);
        });
        // End
    }

    private MyTableModel getModel() {
        MyTableModel tableModel = new MyTableModel();
        db dbcon;
        try {
            dbcon = new db();
            ResultSet rs = dbcon.executeQuery("select * from person");
            ResultSetMetaData rsmd = rs.getMetaData();
            int Colnum = rsmd.getColumnCount();
            int i;
            for (i = 1; i <= Colnum; i++) {
                tableModel.addColumn(rsmd.getColumnName(i));
            }
            ArrayList<PersonEntity> v = new ArrayList<>();
            while (rs.next()) {
                PersonEntity person = new PersonEntity();
                person.setNo(rs.getString("no"));
                person.setName(rs.getString("name"));
                person.setSex(rs.getString("sex"));
                person.setBirthday(rs.getDate("birthday"));
                person.setProfessor(rs.getString("professor"));
                person.setDeptno(rs.getString("deptno"));
                v.add(person);
            }
            rs.close();
            for (i = 0; i < v.size(); i++) {
                tableModel.addRow(new Object[] {
                        v.get(i).getNo(),
                        v.get(i).getName(),
                        v.get(i).getSex(),
                        v.get(i).getBirthday(),
                        v.get(i).getProfessor(),
                        v.get(i).getDeptno()
                });
            }
            dbcon.closeConn();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return tableModel;
    }
    public static void main(String[] args) {
        new personselect().setVisible(true);
    }

}
